﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using VINETLIFE.DO;
using VINETLIFE.BL;
using System.Data;
using System.Web.Caching;

namespace VINETLIFE
{
    public partial class Search : System.Web.UI.Page
    {
        cmsProductBL data = new cmsProductBL();
        cmsProductDO itemProductDO = new cmsProductDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["keyword"]))
            {
                string keyWord = Request.QueryString["keyword"].ToString().Replace("-", " ");
                if (!IsPostBack)
                {
                    if (Session["shop"] == null)
                    {
                        Session["shop"] = new MyShopCart();
                    }
                    SEO(keyWord);
                }
                SimpleSearch(keyWord);
                NoSearch(keyWord);
                ltrBreadcrumb.Text = Ultility.Breadcrumb("search", keyWord);
            }
            else
            {
                Response.Redirect("/");
            }
        }
        protected void SEO(string keyWord)
        {
            //SEO
            this.Title = keyWord.ToString() + " | " + Ultility.WebTitle();

            sysConfigDO objConfig = new sysConfigDO();
            objConfig.ConfigID = 2;

            objConfig = new sysConfigBL().Select(objConfig);


            HtmlMeta metaKey = new HtmlMeta();
            metaKey.Name = "keywords";
            metaKey.Content = objConfig.ConfigValue;
            Page.Header.Controls.Add(metaKey);

            objConfig.ConfigID = 3;
            objConfig = new sysConfigBL().Select(objConfig);
            HtmlMeta metaDesc = new HtmlMeta();
            metaDesc.Name = "description";
            metaDesc.Content = objConfig.ConfigValue;
            Page.Header.Controls.Add(metaDesc);
        }

        void NoSearch(string keyword)
        {
            DataTable tb1 = new cmsArticleBL().SearchByKeyword(keyword);
            DataTable tb2 = new cmsProductBL().SimpleSearch(keyword);
            if (tb1.Rows.Count == 0 && tb2.Rows.Count == 0)
            {
                Literal1.Visible = true;
                Literal1.Text = "Không tìm được sản phẩm nào với từ khóa: <span style=\"color:red;font-weight:bold;\">" + keyword + "</span>";
                Literal2.Text = "Không tìm được sản phẩm nào với từ khóa: <span style=\"color:red;font-weight:bold;\">" + keyword + "</span>";
            }
            else
            {
                no_search.Visible = false;
            }
        }
        private Cache cache = new Cache();
        private void LoadPromotionProduct(DataTable dt,string keyword)
        {
            if (dt.Rows.Count > 0)
            {
                ltProduct.Visible = true;
                ltProduct.Text = "Tìm được <span style=\"color:red;font-weight:bold;\">(" + dt.Rows.Count + ")</span> sản phẩm với từ khóa: <span style=\"color:red;font-weight:bold;\">" + keyword + "</span>";
                CollectionPager1.MaxPages = 10000;
                CollectionPager1.PageSize = 20; // số items hiển thị trên một trang
                CollectionPager1.DataSource = dt.DefaultView;
                CollectionPager1.BindToControl = rptNewProduct;
                rptNewProduct.DataSource = CollectionPager1.DataSourcePaged;
                rptNewProduct.DataBind();
            }
            else
                search_product.Visible = false;
        }
        private void SimpleSearch(string keyword)
        {
            LoadPromotionProduct(new cmsProductBL().SimpleSearch(keyword),keyword);
        }
        protected string FriendlyUrl(string strTitle)
        {
            return Ultility.Change_AV(strTitle);
        }
        public string WordCut(string text)
        {
            return Ultility.WordCut(text, 260, new char[] { ' ', '.', ',', ';' }) + "...";
        }
        protected void rptNewProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                DataRowView drv = (DataRowView)e.Item.DataItem;

                bool isNew = (bool)drv["IsNew"];
                bool isPromotion = (bool)drv["IsPromotion"];
                Int64 price = (Int64)drv["Price"];
                Int64 salePrice = (Int64)drv["SalePrice"];
                double percent = 0;
                try
                {
                    percent = (double)(((price - salePrice) * 100) / price);
                }
                catch { percent = 0; }

                Label lblType = (Label)e.Item.FindControl("lblType");
                Panel pnlPromotion = (Panel)e.Item.FindControl("pnlPromotion");
                Panel pnlNonPromotion = (Panel)e.Item.FindControl("pnlNonPromotion");
                Literal ltrSale = (Literal)e.Item.FindControl("ltrSale");
                pnlPromotion.Visible = false;
                pnlNonPromotion.Visible = true;
                if (isNew)
                {
                    lblType.Text = "New";
                    pnlPromotion.Visible = false;
                    pnlNonPromotion.Visible = true;
                }
                if (isPromotion)
                {
                    lblType.Text = "Sale";
                    pnlPromotion.Visible = true;
                    pnlNonPromotion.Visible = false;
                    ltrSale.Text = percent + "%";
                }
                if (isNew && isPromotion)
                {
                    lblType.Text = "Sale";
                    pnlPromotion.Visible = true;
                    pnlNonPromotion.Visible = false;
                    ltrSale.Text = percent + "%";
                }
            }
        }

    }
}